package com.stanwind.sgit.exec;


import static com.stanwind.sgit.GitCmdConstant.CMD_GIT_LOG;

import com.stanwind.sgit.exception.GitException;
import com.stanwind.sgit.util.StringUtils;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * GitOp
 *
 * @author : Stan
 * @version : 1.0
 * @date :  2019-11-29 14:45
 **/
public class GitLog extends AbsGitOperation {
    public static final Logger logger = LoggerFactory.getLogger(GitLog.class);

    public static final String PATTERN_HASH = "(commit )(.+)";

    private String latestHash;

    @Override
    protected boolean processExecLog(String msg) throws GitException {
        if (StringUtils.isBlank(msg)) {
            logger.error("get blank msg");
            throw new GitException("get blank log msg");
        }


        Pattern pattern = Pattern.compile(PATTERN_HASH);
        Matcher mc = pattern.matcher(msg);
        if (mc.find()) {
            setLatestHash(mc.group(2));
            logger.debug("git hash:" + getLatestHash());
        }

        return true;
    }

    @Override
    public boolean exec(File localRepo) throws GitException {
        return exec(CMD_GIT_LOG, localRepo);
    }

    public static void main(String[] args) throws GitException {
        File localRepo = new File("F:\\gittest\\B\\Base");
        GitLog op = new GitLog();
        op.exec(localRepo);
    }

    public String getLatestHash() {
        return latestHash;
    }

    public void setLatestHash(String latestHash) {
        this.latestHash = latestHash;
    }
}
